home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / file.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  377 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    file -
  19.  *        Miscellaneous support for files.
  20.  *
  21.  *                Paul Haeberli - 1985
  22.  */
  23. #include "stdio.h"
  24. #include "unistd.h"
  25. #include "image.h"
  26. #include "resource.h"
  27. #include "sys/types.h"
  28. #include "sys/stat.h"
  29.  
  30. sizeoffile(f)
  31. FILE *f;
  32. {
  33.     int pos, ret;
  34.  
  35.     pos = ftell(f);
  36.     if ((ret = fseek(f,0,2)) < 0) {
  37.     fprintf(stderr,"sizeoffile: seek error\n");
  38.     exit(1);
  39.     }
  40.     ret = ftell(f);
  41.     if (fseek(f,pos,0) < 0) {
  42.     fprintf(stderr,"sizeoffile: seek error\n");
  43.     exit(1);
  44.     }
  45.     return ret;
  46. }
  47.  
  48. unsigned char *charfiledata(f)
  49. FILE *f;
  50. {
  51.     unsigned char *dat;
  52.     int nbytes;
  53.  
  54.     nbytes = sizeoffile(f);
  55.     if (fseek(f,0,0) < 0) {
  56.     fprintf(stderr,"charfiledata: seek error\n");
  57.     exit(1);
  58.     }
  59.     dat = (unsigned char *)mymalloc(nbytes);
  60.     fread(dat,1,nbytes,f);
  61.     if (fseek(f,0,0) < 0) {
  62.     fprintf(stderr,"charfiledata: seek error\n");
  63.     exit(1);
  64.     }
  65.     return dat;
  66. }
  67.  
  68. int fcopy(inf,outf)
  69. FILE *inf, *outf;
  70. {
  71.     char tmpbuf[10240];
  72.     int nbytes, totbytes;
  73.  
  74.     totbytes = 0;
  75.     while(1) {
  76.     nbytes = fread(tmpbuf,1,10240,inf);
  77.     if(nbytes<=0) 
  78.         break;
  79.     fwrite(tmpbuf,1,nbytes,outf);
  80.     totbytes += nbytes;
  81.     }
  82.     return totbytes;
  83. }
  84.  
  85. char *basename(str)
  86. char *str;
  87. {
  88.     char *cptr;
  89.  
  90.     cptr = str;
  91.     while(*cptr)
  92.     cptr++;
  93.     while(cptr != str) {
  94.     cptr--;
  95.     if(*cptr == '/')
  96.         return (cptr+1);
  97.     }
  98.     return str;
  99. }
  100.  
  101. readfile(inf,buf)
  102. FILE *inf;
  103. char *buf;
  104. {
  105.     int nbytes;
  106.     int count;
  107.  
  108.     count = 0;
  109.     while(1) {
  110.     nbytes = fread(buf,1,10240,inf);
  111.     if(nbytes<=0) 
  112.         break;
  113.     buf += nbytes;
  114.     count += nbytes;
  115.     }
  116.     *buf = 0;
  117.     return count;
  118. }
  119.  
  120. isfile(name)
  121. char *name;
  122. {
  123.     if(access(name,04) == 0)
  124.     return 1;
  125.     else
  126.     return 0;
  127. }
  128.  
  129. static goodfile(name,exec)
  130. char *name;
  131. int exec;
  132. {
  133.     FILE *inf;
  134.     short magic;
  135.  
  136.     inf = fopen(name,"r");
  137.     if(!inf)
  138.     return 0;
  139.     if(exec) {
  140.     magic = 0;
  141.     fread(&magic,2,1,inf);
  142.     fclose(inf);
  143.     if((magic == 0x0160) || (magic == 0x7f45))
  144.         return 1;
  145.     else
  146.         return 0;
  147.     } else {
  148.     fclose(inf);
  149.     return 1;
  150.     }
  151. }
  152.  
  153. static locatefile(name,expname,domain,exec)
  154. char *name, *expname, *domain;
  155. int exec;
  156. {
  157.     char *path;
  158.     char *start, *finish;
  159.     char save;
  160.     char template[256];
  161.  
  162.     if(name[0] == '/' || name[0] == '.') {
  163.     strcpy(expname,name);
  164.     return;
  165.     } 
  166.     if(goodfile(name,exec)) {
  167.     strcpy(expname,name);
  168.     return;
  169.     }
  170.     path = (char *)getenv(domain);
  171.     if(!path) {
  172.     strcpy(expname,name);
  173.     return;
  174.     }
  175.     start = path;
  176.     finish = path;
  177.     while(1) {
  178.     if(*path == 0 || *path == ':') {
  179.         if(start != finish ) {
  180.         save = *finish;
  181.         *finish = 0;
  182.         strcpy(template,start);
  183.         *finish = save;
  184.         strcat(template,"/");
  185.         strcat(template,name);
  186.         if(goodfile(template,exec)) {
  187.             strcpy(expname,template);
  188.             return;
  189.         }
  190.         }
  191.         if(*path == 0) {
  192.         strcpy(expname,name);
  193.         return;
  194.         }
  195.         path++;
  196.         start = path;
  197.         finish = path;
  198.     } else {
  199.         path++;
  200.         finish = path;
  201.     }
  202.     }
  203. }
  204.  
  205. findname(name,expname,domain)
  206. char *name, *expname, *domain;
  207. {
  208.     locatefile(name,expname,domain,0);
  209. }
  210.  
  211. findexec(name,expname,domain)
  212. char *name, *expname, *domain;
  213. {
  214.     locatefile(name,expname,domain,1);
  215. }
  216.  
  217. FILE *gfxopen(name,mode)
  218. char *name, *mode;
  219. {
  220.     char expname[256];
  221.  
  222.     findname(name,expname,"GFXPATH");
  223.     return fopen(expname,mode);
  224. }
  225.  
  226. findfile(name)
  227. {
  228.     char expname[256];
  229.  
  230.     findname(name,expname,"GFXPATH");
  231.     return isfile(expname);
  232. }
  233.  
  234. isimagefile(name)
  235. char *name;
  236. {
  237.     FILE *inf;
  238.     unsigned short magic;
  239.  
  240.     inf = fopen(name,"r");
  241.     if(!inf)
  242.     return 0;
  243.     magic = 0;
  244.     fread(&magic,1,sizeof(short),inf);
  245.     fclose(inf);
  246.     if(magic == IMAGIC)
  247.     return 1;
  248.     magic = (magic>>8)&0xff + (magic<<8)&0xff00;
  249.     if(magic == IMAGIC)
  250.     return 1;
  251.     else
  252.     return 0;
  253. }
  254.  
  255. /*
  256.  *    resource reading stuff follows
  257.  *
  258.  *
  259.  */
  260. #define BACKMAGIC    (('P'<<24)+('A'<<16)+('U'<<8)+('L'<<8))
  261. #define RESSIZE         (4+4+32)
  262.  
  263. extern int __Argc;
  264. extern char **__Argv;
  265.  
  266. static char nametab[40][32];
  267. static int offsettab[40];
  268. static int lengthtab[40];
  269. static int nres, curbase, curlength;
  270. static FILE *resf;
  271.  
  272. static int readrestab()
  273. {
  274.     int i, s;
  275.     long magic;
  276.  
  277.     fseek(resf,-(4+3*4+4+4+32),SEEK_END);
  278.     for(i=0; i<3; i++) {
  279.     s = fseek(resf,-(4+i*4),SEEK_END);
  280.     magic = 0;
  281.     s = fread(&magic,sizeof(long),1,resf);
  282.     if(magic != BACKMAGIC) 
  283.         return 0;
  284.     }
  285.     fseek(resf,-(4+3*4),SEEK_END);
  286.     fread(&nres,sizeof(long),1,resf);
  287.     for(i=0; i<nres; i++) {
  288.     fseek(resf,-(4+3*4+4)-i*RESSIZE,SEEK_END);
  289.     fread(&lengthtab[i],sizeof(long),1,resf);
  290.     fseek(resf,-(4+3*4+4+4)-i*RESSIZE,SEEK_END);
  291.     fread(&offsettab[i],sizeof(long),1,resf);
  292.     fseek(resf,-(4+3*4+4+4+32)-i*RESSIZE,SEEK_END);
  293.     fread(nametab[i],32,1,resf);
  294.     }
  295.     return 1;
  296. }
  297.  
  298. static findres(name)
  299. char *name;
  300. {
  301.     int i;
  302.  
  303.     for(i=0; i<nres; i++) {
  304.     if(strcmp(nametab[i],name) == 0) {
  305.         fseek(resf,offsettab[i],SEEK_SET);
  306.         curbase = offsettab[i];
  307.         curlength = lengthtab[i];
  308.            return 1;
  309.     }
  310.     }
  311.     return 0;
  312. }
  313.  
  314. FILE *res_fopen(name,mode)
  315. char *name, *mode;
  316. {
  317.     char fname[512];
  318.  
  319.     if(name[0] == '#') {
  320.     if(!resf) {
  321.         findexec(__Argv[0],fname,"PATH");
  322.         resf = fopen(fname,"r");
  323.         if(!resf) {
  324.         fprintf(stderr,"res_fopen: can't open input file %s\n",__Argv[0]);
  325.         exit(1);
  326.         }
  327.         if(!readrestab())
  328.         return 0;
  329.     }
  330.     if(findres(name+1))
  331.         return resf;
  332.     fprintf(stderr,"res_fopen: can't find resource %s\n",name+1);
  333.     exit(1);
  334.     } else {
  335.     curbase = 0;
  336.     return fopen(name,mode);
  337.     }
  338. }
  339.  
  340. int res_fread(buf,size,n,inf)
  341. char *buf;
  342. int size, n;
  343. FILE *inf;
  344. {
  345.     return fread(buf,size,n,inf);
  346. }
  347.  
  348. char *res_fgets(buf,size,inf)
  349. char *buf;
  350. int size;
  351. FILE *inf;
  352. {
  353.     return fgets(buf,size,inf);
  354. }
  355.  
  356. int res_fclose(inf)
  357. FILE *inf;
  358. {
  359.     if(!curbase)
  360.     return fclose(inf);
  361.     else
  362.     return 0;
  363. }
  364.  
  365. int res_fseek(f,offset,whence)
  366. FILE *f;
  367. int offset, whence;
  368. {
  369.     return fseek(f,curbase+offset,whence);
  370. }
  371.  
  372. int res_ftell(f)
  373. FILE *f;
  374. {
  375.     return ftell(f)-curbase;
  376. }
  377.